package com.aletto.general_store.ui.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.activity.OnBackPressedCallback;
import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.aletto.general_store.R;

import com.aletto.general_store.presenters.SearchGoodsPageTemplate;
import com.aletto.general_store.presenters.SearchHotTemplate;
import com.aletto.general_store.presenters.SearchKeywordTemplate;
import com.aletto.general_store.presenters.SearchStorePageTemplate;
import com.aletto.general_store.ui.popup.GoodsStoreSwitchPopup;
import com.aletto.general_store.utils.SoftKeyBoardUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;


public class SearchActivity extends AppCompatActivity {

    public static Intent newIntent(Context context, String keyword) {
        Intent intent = new Intent(context, SearchActivity.class);
        intent.putExtra("KEYWORD", keyword);
        return intent;
    }


    private TextView mSwitchTextView;
    private SearchHotTemplate searchHotTemplate;
    private SearchKeywordTemplate searchKeywordTemplate;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        mSwitchTextView = findViewById(R.id.switch_text);
        findViewById(R.id.back_image).setOnClickListener(v -> finish());

        // 初始化
        RecyclerView hotSearchRecycler = findViewById(R.id.recycler_hot_search);
        RecyclerView searchRecycler = findViewById(R.id.search_content_recycler);

        // 热搜
        searchHotTemplate = new SearchHotTemplate(hotSearchRecycler);
        searchHotTemplate.execute(getLifecycle());

        // 搜索结果
        EditText searchContentEdit = findViewById(R.id.search_content_edit);
        searchKeywordTemplate = new SearchKeywordTemplate(searchRecycler, this);
        searchContentEdit.addTextChangedListener(searchKeywordTemplate);

        // 切换
        BasePopupView popup = new XPopup.Builder(this)
                .atView(mSwitchTextView)
                .asCustom(
                        new GoodsStoreSwitchPopup(this)
                                .setOnCheckedChangeListener(this::switchType)
                );

        mSwitchTextView.setOnClickListener(v -> popup.show());

        // 搜索内容回显
        Intent intent = getIntent();
        if (intent != null){
            String keyword = intent.getStringExtra("KEYWORD");
            searchContentEdit.setText(keyword);
        }

        // 弹出键盘
        // SoftKeyBoardUtils.
    }

    private void switchType(RadioGroup group, @IdRes int checkedId) {
        RadioButton checkRadioButton = group.findViewById(checkedId);
        mSwitchTextView.setText(checkRadioButton.getText());

        int searchType = checkedId == R.id.switch_goods_radio
                ? GoodsStoreSwitchPopup.GOODS_TYPE : GoodsStoreSwitchPopup.STORE_TYPE;

        searchHotTemplate.setParams(searchType);
        searchKeywordTemplate.setParams(searchType);
    }

}
